package ru.spb.nicetu.schoolproject.gui;

import ru.spb.nicetu.schoolproject.data.*;

import javax.swing.*;
import java.awt.*;
import java.text.NumberFormat;

/**
 * Created by dchernyshov on 16.04.15.
 */
public class MarkCard extends CardTemplate<Mark, SchoolClass> {
    private JComboBox comboBoxPupils;
    private JComboBox comboBoxSubject;
    private JComboBox comboBoxTeacher;
    private JFormattedTextField markTextField;

    public MarkCard() {
        super(SchoolClass.class, "Оценка", false, new Mark());
        initComboBox();
        Box vbox = createVerticalBox();
        add(vbox, BorderLayout.CENTER);
    }

    public MarkCard(Mark mark) {
        super(SchoolClass.class, "Редактирование оценки", true, mark);
        initComboBox();
        Box vbox = createVerticalBox();
        add(vbox, BorderLayout.CENTER);
        comboBoxPupils.setSelectedItem(mark.getPupil());
        comboBoxSubject.setSelectedItem(mark.getSubject());
        comboBoxTeacher.setSelectedItem(mark.getTeacher());
        markTextField.setValue(mark.getMarkValue());
    }

    public void initComboBox() {
        comboBoxPupils = new JComboBox(new ComboBoxData(Pupil.class).getComboArray());
        comboBoxSubject = new JComboBox(new ComboBoxData(Subject.class).getComboArray());
        comboBoxTeacher = new JComboBox(new ComboBoxData(Teacher.class).getComboArray());
    }

    @Override
    public boolean checkCard() {
        if (markTextField.getText().isEmpty()) {
            errorDialog("Введите оценку!");
            return false;
        }
        return true;
    }

    @Override
    public void inputValue(Mark object) {
        object.setPupil((Pupil)comboBoxPupils.getSelectedItem());
        object.setSubject((Subject)comboBoxSubject.getSelectedItem());
        object.setTeacher((Teacher)comboBoxTeacher.getSelectedItem());
        Number mark = (Number)markTextField.getValue();
        object.setMarkValue(mark.intValue());
    }

    public Box createForthHorizontalBox() {
        Box box = Box.createHorizontalBox();
        JLabel markLabel = new JLabel("Оценка");
        box.add(markLabel);
        box.add(Box.createHorizontalStrut(10));
        markTextField = new JFormattedTextField(NumberFormat.getIntegerInstance());
        markTextField.setColumns(1);
        markTextField.setMaximumSize(
                markTextField.getPreferredSize());
        box.add(markTextField);
        return box;
    }

    public Box createVerticalBox() {
        Box box1 = createHorizontalBox("Ученик", Pupil.class, comboBoxPupils);
        Box box2 = createHorizontalBox("Предмет", Subject.class, comboBoxSubject);
        Box box3 = createHorizontalBox("Учитель", Teacher.class, comboBoxTeacher);
        Box box4 = createForthHorizontalBox();
        Box vbox = Box.createVerticalBox();
        vbox.add(box1);
        vbox.add(Box.createVerticalStrut(10));
        vbox.add(box2);
        vbox.add(Box.createHorizontalStrut(10));
        vbox.add(box3);
        vbox.add(Box.createHorizontalStrut(10));
        vbox.add(box4);
        vbox.add(Box.createHorizontalStrut(10));
        vbox.add(createLastHorizontalBox());
        return vbox;
    }
}
